/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.controllers;

import com.ntb.common.PaginationUtil;
import com.ntb.common.SecuredAction;
import com.ntb.controllers.forms.LandPlanListingForm;
import com.ntb.entities.LandPlansLaid;
import com.ntb.models.LandPlansLaidDAO;
import com.ntb.models.impl.LandPlansLaidDAOImpl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class LandPlanListingAction extends SecuredAction {

    @Override
    public ActionForward ntbExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {

        LandPlanListingForm landPlanListingForm = (LandPlanListingForm) form;

        LandPlansLaidDAO landPlansLaidDAO = new LandPlansLaidDAOImpl();

        String action = landPlanListingForm.getAction();

        List<String> errorMessages = new ArrayList();
        List<String> successMessages = new ArrayList();
        boolean isHaveAction = true;
        if (("delete").equalsIgnoreCase(action)) {
            if (landPlansLaidDAO.delete(landPlanListingForm.getPlanID()) == 0) {
                errorMessages.add("There was an error deleting this land plan. You must delete all lands associated with this land plan before deleting the land plan.");
            } else {
                landPlansLaidDAO.delete(landPlanListingForm.getPlanID());
                successMessages.add("1 land plans out of 1 successfully deleted.");
            }
        } else if (("deletePlans").equalsIgnoreCase(action)) {
            int[] planIDs = landPlanListingForm.getPlanIDs();
            int count = 0;

            int length = 0;

            try {
                length = planIDs.length;
            } catch (Exception ex) {
            }

            if (length > 0) {
                for (int planID : planIDs) {
                    if (landPlansLaidDAO.delete(planID) == 0) {
                        errorMessages.add("There was an error deleting this land plan. You must delete all lands associated with this land plan before deleting the land plan.");
                    } else {
                        landPlansLaidDAO.delete(planID);
                        count++;
                    }
                }
                successMessages.add(count + " land plans out of " + length + " successfully deleted.");
            }
        } else {
            isHaveAction = false;
        }

        String baseUrl = request.getRequestURI() + "?";

        String planName = null;
        int page = 1;

        planName = landPlanListingForm.getName();
        planName = "".equals(planName) ? null : planName;
        if (planName != null) {
            baseUrl += "name=" + planName;
        }

        try {
            int i = Integer.parseInt(request.getParameter("page"));
            page = i > 1 ? i : 1;
        } catch (Exception ex) {
        }

        Iterator<LandPlansLaid> landPlanList = landPlansLaidDAO.getList(planName, page);

        if (isHaveAction) {
            request.setAttribute("errorMessages", errorMessages);
            request.setAttribute("successMessages", successMessages);
        }

        request.setAttribute("landPlanList", landPlanList);

        PaginationUtil paginationUtil = new PaginationUtil();
        paginationUtil.setBaseUrl(baseUrl);
        paginationUtil.setNumPages(landPlansLaidDAO.getPageCount(planName));
        paginationUtil.setCurPage(page);
        request.setAttribute("pagination", paginationUtil.createLinks());

        return mapping.getInputForward();
    }
}
